const Router = require("koa-router")
const util = require("util")
const fs = require("fs")

const router = new Router()

router.get("/api/file", async (ctx, next) => {
    console.log(a);
    const data = await util.promisify(fs.readFile)("./package.json");
    ctx.set('Content-Type', 'text/html; chartset=utf-8');
    ctx.body = data;
})

router.post("/api/user", async (ctx, next) => {
    console.log(typeof ctx.request.body);
    console.log(ctx.request.body);
    ctx.body = ctx.request.body;
})

router.get("/user", async (ctx, next) => {
    ctx.render('user')
})

router.get("/", async (ctx, next) => {
    ctx.render('index')
})

router.get("/api/getData", async (ctx, next) => {
    ctx.body = {
        "data": {
            "cpu": {
                "Core Count": " 4",
                "Current Speed": " 1600 MHz",
                "Manufacturer": " Intel(R) Corporation",
                "usage": Math.random()*100
            },
            "disk": {
                "brand": "",
                "size": "",
                "usage": ""
            },
            "gpu": {
                "brand": ""
            },
            "memory": {
                "brand": "Samsung\n 08C8",
                "frequency": "2400 MT/s",
                "size": "40 GB",
                "usage": Math.random()*100
            },
            "network": [
                {
                    "enp0s31f6": {
                        "ip": "192.168.1.235",
                        "linkStatus": "UP",
                        "macAddr": "8c:16:45:e7:19:02"
                    }
                }
            ],
            "sensor": {
                "electricCurrent": {
                    "vt1": "1.2",
                    "vt2": "2.6"
                },
                "powerStatus": 0,
                "powerWaste": "200",
                "speed": "3200",
                "temperature": {
                    "cpucore": "23.4",
                    "x100": "20"
                },
                "voltage": {
                    "vt1596": "1.2",
                    "vt300": "1.3"
                }
            },
            "threshold": {
                "cpuUsage": "70",
                "diskUsage": "60",
                "memoryUsage": "70",
                "powerWaste": "5",
                "sensorTemperature": "70",
                "sensorVoltage": "5",
                "interval": 10
            }
        },
        "msg": ""
    }
})

router.post("/api/saveThreshold", async (ctx, next) => {
    console.log(ctx.request.body);
    ctx.body = {
        status: 0,
        msg: '保存成功！'
    }
})

router.post("/api/saveSnmp", async (ctx, next) => {
    console.log(ctx.request.body);
    ctx.body = {
        status: 0,
        msg: '保存成功！'
    }
})

router.post("/api/savePower", async (ctx, next) => {
    console.log(ctx.request.body);
    ctx.body = {
        status: 0,
        msg: '保存成功！'
    }
})

router.get("/foo", async (ctx, next) => {
    ctx.redirect("/");
})

module.exports = router